// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import App from './App'
import router from './router'
import FastClick from 'fastclick'
import io from 'socket.io-client'

if ('addEventListener' in document) {
    document.addEventListener('DOMContentLoaded', function() {
        FastClick.attach(document.body);
    }, false);
}
Vue.use(MintUI)
Vue.prototype.GLOBAL = {
	globalMessageTimer : null,
	ws: {}
}
Vue.config.productionTip = false;

import './common/config.js'


// router.beforeEach((to, from, next) => {

//   if(to.name === 'login'){
//   	next()
//   }else{
//   	//登录
//     var isLogin = sessionStorage.user && JSON.parse(sessionStorage.user).login || 0;
//     console.log(isLogin)
//   	if(isLogin){
//   		next()
//   	}
//   	else{
//   		var obj = {
// 	  		Message:true,
// 	  	}
// 	  	if(to.name in obj){

// 	  		next({name: 'Login'})
// 	  	}
//       else{
//         next()
//       }
//   	}

//   }
//   console.log(to);

// })

router.beforeEach((to, from, next) => {

    if(localStorage.user){
      sessionStorage.user = localStorage.user;
    }
	clearInterval(Vue.prototype.GLOBAL.globalMessageTimer);
	Vue.prototype.GLOBAL.ws.close && Vue.prototype.GLOBAL.ws.close();
    console.log(to.meta);
    if(to.meta){
      document.title= to.meta.title || '女神有财';
    }

    // alert(window.location.href);

    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限


      var  ajaxData = {};
      ajaxData.token = localStorage.user && JSON.parse(localStorage.user).token;

      $.ajax({
        type:'post',
        url: RouteMap.peopleDetail,
        data:ajaxData,
        success:function(res){
          if(res.code == 202){

            sessionStorage.user = '';
            localStorage.user = '';


          }

          var user = localStorage.getItem('user') || '';
          sessionStorage.setItem('user',user);

          let isLogin = sessionStorage.user && JSON.parse(sessionStorage.user).login || 0;
          if (isLogin) {  // 通过vuex state获取当前的token是否存在
            next();
          }
          else {
            next({
              path: '/login',
              query: {redirect: from.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
          }

        }
      })

    }
    else {
        next();

    }


})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})
